package com.aimuceliang.aimuceliang.ui.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.aimuceliang.aimuceliang.Constants;
import com.aimuceliang.aimuceliang.bean.WoChengBean;
import com.aimuceliang.aimuceliang.dev.R;
import com.aimuceliang.aimuceliang.green_dao.WoCheng;
import com.aimuceliang.aimuceliang.green_dao.WoChengDaoUtils;
import com.aimuceliang.aimuceliang.ui.BaseActivity;
import com.aimuceliang.aimuceliang.ui.adapter.CommonAdapter;
import com.aimuceliang.aimuceliang.ui.adapter.CommonViewHolder;
import com.aimuceliang.aimuceliang.utils.BluetoothChat;
import com.aimuceliang.aimuceliang.utils.DataUtils;
import com.aimuceliang.aimuceliang.utils.DateUtils;
import com.aimuceliang.aimuceliang.utils.T;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Collections;

import butterknife.BindView;
import butterknife.OnClick;

public class WoChengActivity extends BaseActivity {

    @BindView(R.id.wc_earid)
    EditText wc_earid;
    @BindView(R.id.wc_weight)
    EditText wc_weight;
    @BindView(R.id.wc_total_weight)
    EditText wc_total_weight;
    @BindView(R.id.most_light)
    EditText most_light;
    @BindView(R.id.most_weight)
    EditText most_weight;
    @BindView(R.id.average_weight)
    EditText average_weight;
    @BindView(R.id.wc_listView)
    ListView listView;

    private WoChengDaoUtils daoUtils;
    private MyAdapter adapter;
    private ArrayList<WoCheng> woChengs;
    private DataUtils dataUtils;

    private Handler mHandler = new Handler(){
        @Override public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){

            }
        }
    };

    @Override
    protected int initContentView() {
        return R.layout.activity_wocheng;
    }

    @Override
    protected void initUIAndListener() {
        setTitle("整窝称重");
        Button mBtnNext = (Button) findViewById(R.id.wc_next_btn);
        mBtnNext.setEnabled(false);

        daoUtils = new WoChengDaoUtils(this);
        adapter = new MyAdapter();
        listView.setAdapter(adapter);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        BluetoothChat.getBluetoothChat(0).shutdownClient();
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void initData() {
        woChengs = new ArrayList<>();
        woChengs.addAll(daoUtils.queryAllWoCheng());
        while(woChengs.size() > 3) {
            woChengs.remove(0);
        }
        Collections.reverse(woChengs);
        adapter.bindData(woChengs);
    }

    @OnClick({R.id.wc_next_btn, R.id.wc_data_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.wc_next_btn:
                String earid = wc_earid.getText().toString().trim();
                String weight = wc_weight.getText().toString().trim();
                String total_weight = wc_total_weight.getText().toString().trim();
                String light = most_light.getText().toString().trim();
                String mWeight = most_weight.getText().toString().trim();
                String aWeight = average_weight.getText().toString().trim();
                if (earid.isEmpty()) {
                    T.showShort("耳号不能为空");
                    return;
                }
                WoCheng woCheng = new WoCheng();
                woCheng.setEarid(earid);
                woCheng.setWeight(total_weight);
                woCheng.setWeightavarage(aWeight);
                woCheng.setWeightmax(mWeight);
                woCheng.setWeightmin(light);
                woCheng.setNumber(weight);
                woCheng.setId(null);
                woCheng.setDeviceid(Constants.DEVICE_ID);
                woCheng.setTime(DateUtils.getCurrentFormatTime());
                boolean isInsertSuccess = daoUtils.insertWoCheng(woCheng);
                if (isInsertSuccess) {
                    wc_earid.setText("");
                    wc_weight.setText("");
                    wc_total_weight.setText("");
                    most_light.setText("");
                    most_weight.setText("");
                    average_weight.setText("");
                    woChengs.add(woCheng);

                    while(woChengs.size() > 3) {
                        woChengs.remove(0);
                    }
                    Collections.reverse(woChengs);
                    adapter.notifyDataSetChanged();
                }

                Button mBtnNext = (Button) findViewById(R.id.wc_next_btn);
                mBtnNext.setEnabled(false);

                break;
            case R.id.wc_data_btn:
                startActivityForResult(new Intent(WoChengActivity.this, WCDataActivity.class), 1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(1 == resultCode) {
            woChengs.clear();
            adapter.notifyDataSetChanged();
        }
    }

    // Called in Android UI's main thread
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessage(WoChengBean event) {
        WoCheng woCheng = new WoCheng();

        woCheng.setEarid(event.strEarNum);
        woCheng.setNumber(event.strNumber);
        woCheng.setWeight(event.strWeightAll);
        woCheng.setWeightmin(event.strWeightMin);
        woCheng.setWeightmax(event.strWeightMax);
        woCheng.setWeightavarage(event.strWeightAvg);

        //wc_earid.setText(event.strEarNum);
        wc_weight.setText(event.strNumber);
        wc_total_weight.setText(event.strWeightAll);
        most_light.setText(event.strWeightMin);
        most_weight.setText(event.strWeightMax);
        average_weight.setText(event.strWeightAvg);

        if(event.strNumber.equals("0")) {
            Button mBtnNext = (Button) findViewById(R.id.wc_next_btn);
            mBtnNext.setEnabled(false);
        } else {
            Button mBtnNext = (Button) findViewById(R.id.wc_next_btn);
            mBtnNext.setEnabled(true);
        }

    }

    private class MyAdapter extends CommonAdapter<WoCheng> {
        MyAdapter() {
            super(R.layout.item_wocheng_measure, WoChengActivity.this);
        }

        @Override
        public void convert(CommonViewHolder holder, WoCheng woCheng) {
            //初始化点击事件,防止复用导致点击出错
            holder.getView(R.id.wc_delete).setOnClickListener(null);
            holder.setText(R.id.wc_ear_id, woCheng.getEarid());
            holder.setText(R.id.wc_weight, woCheng.getWeight());
            holder.setText(R.id.wc_num, woCheng.getNumber());

            holder.getView(R.id.wc_delete).setOnClickListener(v -> {
                daoUtils.deleteWoCheng(woCheng);
                woChengs.clear();
                woChengs.addAll(daoUtils.queryAllWoCheng());
                while(woChengs.size() > 3) {
                    woChengs.remove(0);
                }
                Collections.reverse(woChengs);
                adapter.notifyDataSetChanged();
            });
        }
    }
}
